package com.qxsx.midi.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.qxsx.midi.R;
import com.qxsx.midi.activity.study.MidiManDetailsActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.model.Buy;
import com.qxsx.midi.model.Teacher;
import com.qxsx.midi.utils.BitmapCache;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.view.CircleImageView;

import java.util.List;

/**
 * Created by Joney on 2015/9/7.
 */
public class TeacherAdapter extends BaseAdapter {

    private Context context;

    private List<Teacher> lists;
    private RequestQueue queue;
    private ImageLoader imageLoader;
    public TeacherAdapter(Context context, List<Teacher> lists) {
        this.context = context;
        this.lists = lists;
        queue = Volley.newRequestQueue(context);
        imageLoader = new ImageLoader(queue, new BitmapCache());
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    //动态添加数据
    public void addData(List<Teacher> addlist) {//刷新数据
        for (Teacher teacher : addlist) {
            lists.add(teacher);
        }
        notifyDataSetChanged();
    }
    // 清空所有
    public void removeAll()
    {
        lists.clear();
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.exam_teacher_item, null);
            viewHolder.iv_head = (CircleImageView) convertView.findViewById(R.id.iv_head);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_level = (TextView) convertView.findViewById(R.id.tv_level);
            viewHolder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
            viewHolder.tv_major_name = (TextView) convertView.findViewById(R.id.tv_major_name);
            viewHolder.tv_city = (TextView) convertView.findViewById(R.id.tv_city);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final Teacher teacher = lists.get(position);
        viewHolder.tv_name.setText(teacher.getTeacher_naem());
        viewHolder.tv_level.setText(teacher.getTeacher_highest_level());
        viewHolder.tv_major_name.setText(teacher.getTeacher_major_name());
        viewHolder.tv_desc.setText(teacher.getTeacher_introduce());
        viewHolder.tv_city.setText(teacher.getTeacher_city());
        //MyNet.setNetImage(AppContext.getContext(), viewHolder.iv_head, teacher.getTeacher_head_img());
        viewHolder.iv_head.setDefaultImageResId(R.mipmap.head_ico);
        viewHolder.iv_head.setErrorImageResId(R.mipmap.head_ico);
        viewHolder.iv_head.setImageUrl(teacher.getTeacher_head_img(), imageLoader);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(context, MidiManDetailsActivity.class);
                it.putExtra("id", teacher.getTeacher_id());//传递商家ID
                context.startActivity(it);
            }
        });

        return convertView;
    }

    static class ViewHolder {

        private CircleImageView iv_head;

        private TextView tv_name;

        private TextView tv_level;

        private TextView tv_desc;

        private TextView tv_major_name;

        private TextView tv_city;
    }
}
